package hoowin.app.wsnd.dao.springImp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import hoowin.app.wsnd.dao.IUpdownDao;
import hoowin.app.wsnd.model.Updown;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class UpdownDao extends JdbcDaoSupport implements IUpdownDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List getUpdownListByTranslationId(int id) {
		String sql = "select user_id, up_value from apps_wsnd_updowns where translation_id="+id;
		return super.getJdbcTemplate().query(sql, new UpdownRowMapper());
	}

	@Override
	public boolean addUpdown(int translationId, int userId, int upValue) {
		String sql = "insert into apps_wsnd_updowns (translation_id, user_id, up_value) values (?, ?, ?)";
		if(super.getJdbcTemplate().update(sql, translationId, userId, upValue)>0){
			return true;
		}else return false;
	}
	
	class UpdownRowMapper implements RowMapper<Object>{
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Updown updown = new Updown();
			updown.userId = rs.getInt("user_id");
			updown.upValue = rs.getInt("up_value");
			return updown;
		}	
	}
}
